//
//  MBDiscountAlertView.swift
//  StarTimesON
//
//  Created by Toj on 10/9/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit
import StarKit

class MBDiscountAlertView: UIView {
    
    func show(in view: UIView? = nil) {
        var view_ = view
        if view_ == nil {
            let appDelegate = UIApplication.shared.delegate as? AppDelegate
            view_ = appDelegate?.window
        }
        
        guard let superview = view_ else {
            close()
            return
        }
        superview.addSubview(self)
        snp.makeConstraints { make in
            make.edges.equalTo(superview)
        }
        
        setupLayoutSubviews()
    }
    
    func close() {
        isHidden = true
        removeFromSuperview()
    }
    
    @objc private func didTapClose() {
        close()
    }
    
    @objc private func didTapIknow() {
        close()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(backgroundView)
        addSubview(contentView)
        contentView.addSubview(textView)
        contentView.addSubview(iknowButton)
    }
    
    private func setupLayoutSubviews() {
        backgroundView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        let maxWidth = 260.ui_mscale
        let textHeight = textView.text.st_stringHeight(font: UIFont.systemFont(ofSize: 13), maxWidth: maxWidth) + 20
        let maxHeight = STScreenHeight * 0.3
        let textViewHeight = textHeight > maxHeight ? maxHeight : textHeight
        let contentHeight = textViewHeight + 75.ui_scale
        contentView.snp.makeConstraints { make in
            make.center.equalTo(self)
            make.width.equalTo(280.ui_mscale)
            make.height.equalTo(contentHeight)
        }
        
        textView.snp.makeConstraints { make in
            make.top.equalTo(20.ui_scale)
            make.centerX.equalTo(contentView)
            make.height.equalTo(textViewHeight)
            make.width.equalTo(maxWidth)
        }
        
        iknowButton.snp.makeConstraints { make in
            make.right.bottom.equalTo(contentView).inset(10.ui_scale)
            make.height.equalTo(25.ui_scale)
            make.width.equalTo(50)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    private lazy var backgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.black.st_alpha(0.25)
        let tap = UITapGestureRecognizer(target: self, action: #selector(didTapClose))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    private(set) lazy var textView: UITextView = {
        let view = UITextView()
        view.font = UIFont.systemFont(ofSize: 13)
        view.textColor = STColor.olBlack6
        return view
    }()
    
    private lazy var iknowButton: UIButton = {
        let view = UIButton()
        view.titleLabel?.font = UIFont.bold(with: 13.ui_scale)
        view.setTitleColor(STColor.olBlue, for: .normal)
        view.setTitle("i_know_capital".localized(), for: .normal)
        view.addTarget(self, action: #selector(didTapIknow), for: .touchUpInside)
        return view
    }()
}
